﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JYStockAnalyser.Face;
using JYStockAnalyser.Model;
using JYStockAnalyser.ClientLib;

namespace JYStockAnalyser.WinUI
{
    public class PeriodInputer : UserControl
    {
        private ComboBox ddlEndYears;
        private Label label9;
        private ComboBox ddlBeginYears;
        private ComboBox ddlAnalyseTimeType;
        private Label label5;
        private FlowLayoutPanel flowLayoutPanel1;
        private Label lblPeriod;

        public PeriodInputer()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            this.ddlEndYears = new System.Windows.Forms.ComboBox();
            this.label9 = new System.Windows.Forms.Label();
            this.ddlBeginYears = new System.Windows.Forms.ComboBox();
            this.ddlAnalyseTimeType = new System.Windows.Forms.ComboBox();
            this.label5 = new System.Windows.Forms.Label();
            this.lblPeriod = new System.Windows.Forms.Label();
            this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
            this.flowLayoutPanel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // ddlEndYears
            // 
            this.ddlEndYears.BackColor = System.Drawing.Color.LightGoldenrodYellow;
            this.ddlEndYears.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.ddlEndYears.Font = new System.Drawing.Font("SimSun", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.ddlEndYears.FormattingEnabled = true;
            this.ddlEndYears.Location = new System.Drawing.Point(347, 3);
            this.ddlEndYears.Name = "ddlEndYears";
            this.ddlEndYears.Size = new System.Drawing.Size(90, 22);
            this.ddlEndYears.TabIndex = 55;
            // 
            // label9
            // 
            this.label9.AutoSize = true;
            this.label9.ForeColor = System.Drawing.Color.Aquamarine;
            this.label9.Location = new System.Drawing.Point(324, 7);
            this.label9.Margin = new System.Windows.Forms.Padding(3, 7, 3, 0);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(17, 12);
            this.label9.TabIndex = 54;
            this.label9.Text = "～";
            // 
            // ddlBeginYears
            // 
            this.ddlBeginYears.BackColor = System.Drawing.Color.LightGoldenrodYellow;
            this.ddlBeginYears.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.ddlBeginYears.Font = new System.Drawing.Font("SimSun", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.ddlBeginYears.FormattingEnabled = true;
            this.ddlBeginYears.Location = new System.Drawing.Point(228, 3);
            this.ddlBeginYears.Name = "ddlBeginYears";
            this.ddlBeginYears.Size = new System.Drawing.Size(90, 22);
            this.ddlBeginYears.TabIndex = 53;
            // 
            // ddlAnalyseTimeType
            // 
            this.ddlAnalyseTimeType.BackColor = System.Drawing.Color.LightGoldenrodYellow;
            this.ddlAnalyseTimeType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.ddlAnalyseTimeType.Font = new System.Drawing.Font("SimSun", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.ddlAnalyseTimeType.FormattingEnabled = true;
            this.ddlAnalyseTimeType.Location = new System.Drawing.Point(50, 3);
            this.ddlAnalyseTimeType.Name = "ddlAnalyseTimeType";
            this.ddlAnalyseTimeType.Size = new System.Drawing.Size(96, 22);
            this.ddlAnalyseTimeType.TabIndex = 52;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.ForeColor = System.Drawing.Color.Aquamarine;
            this.label5.Location = new System.Drawing.Point(169, 7);
            this.label5.Margin = new System.Windows.Forms.Padding(20, 7, 3, 0);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(53, 12);
            this.label5.TabIndex = 51;
            this.label5.Text = "时间段：";
            // 
            // lblPeriod
            // 
            this.lblPeriod.AutoSize = true;
            this.lblPeriod.ForeColor = System.Drawing.Color.Aquamarine;
            this.lblPeriod.Location = new System.Drawing.Point(3, 7);
            this.lblPeriod.Margin = new System.Windows.Forms.Padding(3, 7, 3, 0);
            this.lblPeriod.Name = "lblPeriod";
            this.lblPeriod.Size = new System.Drawing.Size(41, 12);
            this.lblPeriod.TabIndex = 50;
            this.lblPeriod.Text = "周期：";
            // 
            // flowLayoutPanel1
            // 
            this.flowLayoutPanel1.Controls.Add(this.lblPeriod);
            this.flowLayoutPanel1.Controls.Add(this.ddlAnalyseTimeType);
            this.flowLayoutPanel1.Controls.Add(this.label5);
            this.flowLayoutPanel1.Controls.Add(this.ddlBeginYears);
            this.flowLayoutPanel1.Controls.Add(this.label9);
            this.flowLayoutPanel1.Controls.Add(this.ddlEndYears);
            this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.flowLayoutPanel1.Name = "flowLayoutPanel1";
            this.flowLayoutPanel1.Size = new System.Drawing.Size(469, 29);
            this.flowLayoutPanel1.TabIndex = 56;
            // 
            // PeriodInputer
            // 
            this.BackColor = System.Drawing.Color.Black;
            this.Controls.Add(this.flowLayoutPanel1);
            this.Name = "PeriodInputer";
            this.Size = new System.Drawing.Size(452, 27);
            this.Load += new System.EventHandler(this.PeriodInputer_Load);
            this.flowLayoutPanel1.ResumeLayout(false);
            this.flowLayoutPanel1.PerformLayout();
            this.ResumeLayout(false);

        }

        private void PeriodInputer_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                m_init();
            }
        }

        bool m_onlyYear = false;

        [System.ComponentModel.Browsable(true)]
        public bool OnlyYear
        {
            get { return m_onlyYear; }
            set { m_onlyYear = value; }
        }

        public string AnalyseTimeType
        {
            get
            {
                return (string)this.ddlAnalyseTimeType.SelectedItem;
            }
        }

        public YearQuarter BeginQuarter
        {
            get
            {
                return (YearQuarter)this.ddlBeginYears.SelectedItem;
            }
        }

        public YearQuarter EndQuarter
        {
            get
            {
                return (YearQuarter)this.ddlEndYears.SelectedItem;
            }
        }

        void m_init()
        {
            this.ddlAnalyseTimeType.Items.Clear();
            if (OnlyYear)
            {
                this.ddlAnalyseTimeType.Visible = false;
                this.lblPeriod.Visible = false;
                this.ddlAnalyseTimeType.Items.Add("年");        
                this.ddlAnalyseTimeType.SelectedIndexChanged += new System.EventHandler(this.ddlAnalyseTimeType_SelectedIndexChanged);
                this.ddlAnalyseTimeType.SelectedIndex = 0;
            }
            else
            {
                this.ddlAnalyseTimeType.Items.Add("年");
                this.ddlAnalyseTimeType.Items.Add("季度");
                this.ddlAnalyseTimeType.SelectedIndexChanged += new System.EventHandler(this.ddlAnalyseTimeType_SelectedIndexChanged);
                this.ddlAnalyseTimeType.SelectedIndex = 1;
            }
        }

        private void ddlAnalyseTimeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlBeginYears.Items.Clear();
            this.ddlEndYears.Items.Clear();

            YearQuarter[] minAndMaxQuarters = ClientCache.Instance.MinAndMaxQuarter;

            string type = AnalyseTimeType;

            if (type.Equals("年"))
            {
                for (int i = minAndMaxQuarters[0].Year; i <= minAndMaxQuarters[1].Year; i++)
                {
                    this.ddlBeginYears.Items.Add(new YearQuarter(i, YearQuarter.NULL_QUARTER));
                    this.ddlEndYears.Items.Add(new YearQuarter(i, YearQuarter.NULL_QUARTER));
                }
            }
            else if (type.Equals("季度"))
            {
                for (int i = minAndMaxQuarters[0].Year; i <= minAndMaxQuarters[1].Year; i++)
                {
                    for (int quarter = 1; quarter <= 4; quarter++)
                    {
                        if (i.Equals(minAndMaxQuarters[1].Year) && quarter > minAndMaxQuarters[1].Quarter) { break; }

                        this.ddlBeginYears.Items.Add(new YearQuarter(i, quarter));
                        this.ddlEndYears.Items.Add(new YearQuarter(i, quarter));
                    }
                }
            }

            this.ddlBeginYears.SelectedIndex = 0;
            this.ddlEndYears.SelectedIndex = this.ddlEndYears.Items.Count - 1;
        }
    }
}
